package com.zhoug.zmui3.keyboard;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.TextPaint;
import android.util.AttributeSet;

import com.zhoug.common3.utils.ColorUtils;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.ResourceUtils;

import java.util.List;

/**
 * 自定义软键盘容器
 *
 * @Author 35574
 * @Date 2021/2/2
 * @Description
 */
public class ZMUIKeyboardView extends KeyboardView {
    private static final String TAG = ">>>ZMUIKeyboardView";
    private static final boolean DEBUG = false;
    /**
     * {@link com.zhoug.zmui3.keyboard.KeyboardUtils.KeyboardType#NUMBER}
     * {@link com.zhoug.zmui3.keyboard.KeyboardUtils.KeyboardType#LETTER}
     * {@link com.zhoug.zmui3.keyboard.KeyboardUtils.KeyboardType#LETTER_NUMBER}
     * {@link com.zhoug.zmui3.keyboard.KeyboardUtils.KeyboardType#CHARACTER}
     */
    private int keyboardType;

    private Drawable mKeyBackground;
    private int mKeyTextColor;
    private float mKeyTextSize;

    private Paint mPaint;
    private TextPaint mTextPaint;
    private Paint.FontMetrics mFontMetrics=new Paint.FontMetrics();



    private static final int COLOR_DELETE = 0XFFFFBBBB;//删除键背景色
    private static final int COLOR_DONE = 0XFFAAFFCC;//确定键背景色
    private static final int COLOR_OTHER = 0XFFDDDDDD;//非主键背景色
    private static final int COLOR_TYPE= 0XFFADDCFC;//切换软键盘按键背景色

    public ZMUIKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);

    }

    public ZMUIKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        int[] attrIds = new int[]{android.R.attr.keyBackground, android.R.attr.keyTextColor, android.R.attr.keyTextSize};
        TypedArray a = context.obtainStyledAttributes(attrs, attrIds, defStyleAttr, 0);

        mKeyBackground = a.getDrawable(0);
        mKeyTextColor = a.getColor(1, 0xff333333);
        mKeyTextSize = a.getDimension(2, ResourceUtils.spTopx(14));
        a.recycle();
        if (DEBUG) {
            LogUtils.d(TAG, "init:mKeyBackground=" + mKeyBackground);
            LogUtils.d(TAG, "init:mKeyTextColor=" + ColorUtils.toHexString(mKeyTextColor));
            LogUtils.d(TAG, "init:mKeyTextSize=" + mKeyTextSize);
        }

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(mKeyTextSize);
        mTextPaint.setFakeBoldText(true);
        mTextPaint.setColor(mKeyTextColor);


    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Keyboard keyboard = getKeyboard();


        if (keyboard == null) return;
        List<Keyboard.Key> keys = keyboard.getKeys();
        if (keys != null && keys.size() > 0) {
            for (Keyboard.Key key : keys) {
                int code = key.codes[0];
                if (code== KeyboardUtils.KEYCODE_DELETE) {
                    //删除键
                    drawKey(canvas,key,COLOR_DELETE);
                }else if(code == KeyboardUtils.KEYCODE_DONE){
                    //确定键
                    drawKey(canvas,key,COLOR_DONE);
                }else  if(code==KeyboardUtils.KEYCODE_TYPE_123 ||code==KeyboardUtils.KEYCODE_TYPE_ABC ||code==KeyboardUtils.KEYCODE_TYPE_CHARACTER){
                    //切换键
                    drawKey(canvas,key,COLOR_TYPE);
                }else{
                    switch (keyboardType){
                        case KeyboardUtils.KeyboardType.LETTER_NUMBER:
                            if(!((code>=48 && code<=57) || (code>=97 && code<=122) ||(code>=65&& code<=90))){
                                drawKey(canvas,key,COLOR_OTHER);
                            }
                            break;
                        case KeyboardUtils.KeyboardType.NUMBER:
                            if(!(code>=48 && code<=57)){
                                drawKey(canvas,key,COLOR_OTHER);
                            }
                            break;

                    }
                }
            }
        }
    }

    /**
     * 修改背景
     * @param canvas
     * @param key
     * @param bgColor
     */
    private void drawKey(Canvas canvas, Keyboard.Key key,int bgColor){
        int  keyLeft = key.x + getPaddingStart();
        int keyTop = key.y + getPaddingTop();
        mKeyBackground.setColorFilter(bgColor, PorterDuff.Mode.SRC_IN);
        mKeyBackground.setBounds(keyLeft, keyTop, keyLeft + key.width, keyTop + key.height);
        mKeyBackground.draw(canvas);
        float  textX=(keyLeft+keyLeft+key.width)>>1;
        int centerY=(keyTop+keyTop+key.height)>>1;
        mTextPaint.getFontMetrics(mFontMetrics);
        float  textBaseLine=centerY-(mFontMetrics.descent+mFontMetrics.ascent)/2;
        canvas.drawText(key.label, 0, key.label.length(), textX,textBaseLine, mTextPaint);


    }

    public void setKeyboardType(int keyboardType) {
        this.keyboardType = keyboardType;
    }
}
